package com.tuanzi.loan.web.bean.settlement;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.settlement.CompanyInfo;
import com.tuanzi.loan.business.entity.settlement.FirstpartyDataChangeRecord;
import com.tuanzi.loan.business.service.family.FirstPartyPrepaymentService;
import com.tuanzi.loan.business.service.settlement.CompanyInfoService;
import com.tuanzi.loan.business.service.settlement.FirstpartyDataChangeRecordService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.settlement.FirstpartyDataChangeRecordInfoRequest;
import com.tuanzi.loan.vo.request.settlement.FirstpartyDataChangeRecordRequest;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.loan.web.lazy.settlement.FirstpartyDataLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.RowEditEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.COOPERATION_MODE;
import static com.tuanzi.loan.business.constants.DictionaryCode.SETTLEMENT_SPREAD_CHANNEL;

/**
 * @author caiwanlin
 * @create 2018-01-18 10:49
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class FirstpartyDataInfoBean
        extends JSFInfoBean<FirstpartyDataChangeRecordInfoRequest> {
    private List<FirstpartyDataChangeRecord> firstpartyDataChangeRecordList;
    private Integer copMode;
    @Autowired
    private FirstpartyDataChangeRecordService firstpartyDataChangeRecordService;

    @Autowired
    private CompanyInfoService companyInfoService;

    private List<CompanyInfo> companyInfoList;

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new FirstpartyDataChangeRecordInfoRequest();
        firstpartyDataChangeRecordList = new ArrayList<>();
        companyInfoList = companyInfoService.findAllCompanyName();
    }

    public void onSearch() {
        firstpartyDataChangeRecordList = firstpartyDataChangeRecordService.findFirstpartyInfo($);
        if (firstpartyDataChangeRecordList.size() == 0) {
            firstpartyDataChangeRecordList = new ArrayList<>();
            addWarnMessage("没有此产品的数据");
        } else {
            FirstpartyDataChangeRecord firstpartyDataChangeRecord = firstpartyDataChangeRecordList.get(0);
            copMode = firstpartyDataChangeRecord.getCopMode();

        }
    }

    public void onSubmit() {
        List<FirstpartyDataChangeRecord> collect = firstpartyDataChangeRecordList.parallelStream().filter(s -> {
            if (s.getIsEdit() == 1) {
                return true;
            } else {
                return false;
            }
        }).collect(Collectors.toList());
        if (collect.size() > 0) {
            firstpartyDataChangeRecordService.saveDataChangeRecord(firstpartyDataChangeRecordList);
            addSuccessfullyMessage();
        }
        else {
            addWarnMessage("您没有修改内容，提交无效");
        }
    }

    public void onRowEdit(RowEditEvent event) {
        FirstpartyDataChangeRecord record = (FirstpartyDataChangeRecord) event.getObject();
        Integer copMode = record.getCopMode();//合作模式
        Integer oldRegisterCount = record.getOldRegisterCount();
        Integer newRegisterCount = record.getNewRegisterCount();
        Double oldLoanAmount = record.getOldLoanAmount();
        Double newLoanAmount = record.getNewLoanAmount();
        Integer oldClickCount = record.getOldClickCount();
        Integer newClickCount = record.getNewClickCount();
        Double settlementPercentPrice = record.getSettlementPercentPrice();
        Double settlementYuanPrice = record.getSettlementYuanPrice();
        Integer oldSpreadChannel = record.getOldSpreadChannel();
        Integer newSpreadChannel = record.getNewSpreadChannel();
        //推广渠道有没有改变
        if (newSpreadChannel != null && oldSpreadChannel != newSpreadChannel) {
            record.setIsEdit(1);
        } else {
            record.setNewSpreadChannel(null);
            record.setIsEdit(0);
        }
        //判断是那种合作模式
        if (copMode == 0) {
            if (newRegisterCount != null && oldRegisterCount != newRegisterCount) {
                record.setNewSettlementAmount(newRegisterCount * settlementYuanPrice);
                record.setIsEdit(1);
            }
        } else if (copMode == 1) {
            if (newLoanAmount != null && oldLoanAmount != newLoanAmount) {
                record.setNewSettlementAmount(newLoanAmount * settlementPercentPrice / 100);
                record.setIsEdit(1);
            }
        } else if (copMode == 2) {
            if (newClickCount != null && oldClickCount != newClickCount) {
                record.setNewSettlementAmount(newClickCount * settlementYuanPrice);
                record.setIsEdit(1);
            }
        } else if (copMode == 3) {
            if (newRegisterCount != null
                    && newLoanAmount != null
                    && oldRegisterCount != newRegisterCount
                    && oldLoanAmount != newLoanAmount) {
                record.setNewSettlementAmount(newRegisterCount * settlementYuanPrice + newLoanAmount * settlementPercentPrice / 100);
                record.setIsEdit(1);
            } else if (newRegisterCount != null
                    && newLoanAmount == null
                    && oldRegisterCount != newRegisterCount) {
                record.setNewSettlementAmount(newRegisterCount * settlementYuanPrice + oldLoanAmount * settlementPercentPrice / 100);
                record.setIsEdit(1);
            } else if (newRegisterCount == null
                    && newLoanAmount != null
                    && oldLoanAmount != newLoanAmount) {
                record.setNewSettlementAmount(oldRegisterCount * settlementYuanPrice + newLoanAmount * settlementPercentPrice / 100);
                record.setIsEdit(1);
            }
        }

    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(COOPERATION_MODE, SETTLEMENT_SPREAD_CHANNEL);
    }
}
